$(function(){
    "use strict";
    let href = window.location.href;
    let hrefINdex = href.indexOf('=');
    let pro_id = href.substring(hrefINdex+1);
    let Completed_file = []

    console.log(pro_id)
    if(pro_id){
        $.ajax({
            url:'http://jk.com/Admin/ProCheck/check',
            type:'get',
            data:{
                id:pro_id
            },
            success:function(data){
                console.log(data)
                if(data.code==1){
                    if(data.status==0){
                        $('.CompanyInfoList input').attr('readonly',false);
                        $('.CompanyInfoList input').addClass('active');
                        $(this).html('保存');
                        $('.textarea_box').children('textarea').addClass('active');
                        $('.textarea_box').children('p').removeClass('active');
                        $('.file_box').children('div').addClass('active')
                        $('.file_box').children('a').removeClass('active')

                        $('#choose_time').children('input').addClass('time_choose');
                        $('#choose_time').children('input').attr('disabled',false);
                        laydate.render({
                            elem: '.time_choose', //指定元素
                            theme: '#2881C1',
                            // show: true, //直接显示
                            closeStop: '#choose_time',
                            done: function(value, date, endDate){
//                var dateVal = date.year+'年'+date.month+'月'
//                window.query_time = dateVal
                            }
                        });

                    }else{
                        $('.CompanyInfoList input').attr('readonly',true);
                        $('.CompanyInfoList input').removeClass('active');
                        $(this).html('编辑');
                        // var conclusion = $('.textarea_box>textarea.conclusion').val();
                        // var content = $('.textarea_box>textarea.content').val();
                        // $('.textarea_box>textarea.conclusion').siblings('p').html(conclusion);
                        // $('.textarea_box>textarea.content').siblings('p').html(content);
                        $('.textarea_box>p').addClass('active');
                        $('.textarea_box>textarea').removeClass('active');

                        // $('.file_box').children('div').removeClass('active')
                        // $('.file_box').children('a').addClass('active')
                        // var fileVal = $('.hidden_input').parent().siblings('span').html()

                        let datas = data.data
                        $('.CompanyInfoList').find('[data-value=Completed_time]').val(datas.check_time)
                        $('.CompanyInfoList').find('[data-value=Completed_place]').val(datas.check_addr)
                        $('.CompanyInfoList').find('[data-value=Completed_people]').val(datas.check_peo)
                        $('.CompanyInfoList').find('[data-value=Completed_money]').val(datas.check_total)
                        $('.CompanyInfoList').find('textarea[data-value=Completed_conent]').val(datas.check_content)
                        $('.CompanyInfoList').find('p[data-value=Completed_conent]').html(datas.check_content)
                        $('.CompanyInfoList').find('textarea[data-value=Completed_conclusion]').val(datas.check_res)
                        $('.CompanyInfoList').find('p[data-value=Completed_conclusion]').html(datas.check_res)
                        $('.CompanyInfoList').find('[data-value=Completed_file]').parent().siblings('span').html(datas.check_fname+'<i class="iconfont icon-delete delete_logo"></i>')
                        $('.file_box').children('a').html(datas.check_fname)
                        $('.file_box').children('a').attr('href',datas.check_fkey)

                        Completed_file = []
                        Completed_file.push(datas.base64)
                        Completed_file.push(datas.check_fname)
                        Completed_file.push(datas.check_fsize)

                        $('#choose_time').children('input').removeClass('time_choose');
                        $('#choose_time').children('input').attr('disabled',true);
                    }
                }else{
                    $('#yz_modal').html(data.msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                }
            }
        })
    }



    // 编辑和保存来回切换
    $('.modifyInfo_btn').click(function(e){
        e.preventDefault();
        $('.CompanyInfoList input').toggleClass('active');
        if($('.CompanyInfoList input').hasClass('active')){
            $('.CompanyInfoList input').attr('readonly',false);
            $(this).html('保存');
            $('.textarea_box').children('textarea').addClass('active');
            $('.textarea_box').children('p').removeClass('active');
            $('.file_box').children('div').addClass('active')
            $('.file_box').children('a').removeClass('active')

            $('#choose_time').children('input').addClass('time_choose');
            $('#choose_time').children('input').attr('disabled',false);
            laydate.render({
                elem: '.time_choose', //指定元素
                theme: '#2881C1',
                // show: true, //直接显示
                closeStop: '#choose_time',
                done: function(value, date, endDate){
//                var dateVal = date.year+'年'+date.month+'月'
//                window.query_time = dateVal
                }
            });

        }else{
            $('.CompanyInfoList input').attr('readonly',true);
            $(this).html('编辑');
            var conclusion = $('.textarea_box>textarea.conclusion').val();
            var content = $('.textarea_box>textarea.content').val();
            $('.textarea_box>textarea.conclusion').siblings('p').html(conclusion);
            $('.textarea_box>textarea.content').siblings('p').html(content);
            $('.textarea_box>p').addClass('active');
            $('.textarea_box>textarea').removeClass('active');

            $('.file_box').children('div').removeClass('active')
            $('.file_box').children('a').addClass('active')
            var fileVal = $('.hidden_input').parent().siblings('span').html()
            $('.file_box').children('a').html(fileVal)

            $('#choose_time').children('input').removeClass('time_choose');
            $('#choose_time').children('input').attr('disabled',true);

            let Completed_time = $('.CompanyInfoList').find('[data-value=Completed_time]').val()
            let Completed_place = $('.CompanyInfoList').find('[data-value=Completed_place]').val()
            let Completed_people = $('.CompanyInfoList').find('[data-value=Completed_people]').val()
            let Completed_money = $('.CompanyInfoList').find('[data-value=Completed_money]').val()
            let Completed_conent = $('.CompanyInfoList').find('[data-value=Completed_conent]').val()
            let Completed_conclusion = $('.CompanyInfoList').find('[data-value=Completed_conclusion]').val()
            // let Completed_file = $('.CompanyInfoList').find('[data-value=Completed_file]').val()
            if(Completed_time == '' || Completed_place == '' || Completed_people == '' || Completed_money == '' || Completed_conent == '' || Completed_conclusion == '' || Completed_file.length<=0){
                $('#yz_modal').html('验收失败')
                $('#yz_modal').addClass('active')
                tipShow()
            }else{
                $.ajax({
                    url:'http://jk.com/Admin/ProCheck/check',
                    type:'post',
                    data:{
                        pro_id:pro_id,
                        check_time: Completed_time,
                        check_addr: Completed_place,
                        check_peo: Completed_people,
                        check_content: Completed_conent,
                        check_res: Completed_conclusion,
                        check_fkey:Completed_file[0],
                        check_fname:Completed_file[1],
                        check_fsize:Completed_file[2],
                        check_total:Completed_money
                    },
                    success:function(data){
                        if(data.code==1){
                            window.location.href='lu_AchievementCompleted_index.html'
                            $('#yz_modal').html('验收成功')
                            $('#yz_modal').addClass('active')
                            tipShow()


                        }else{
                            $('#yz_modal').html(data.msg)
                            $('#yz_modal').addClass('active')
                            tipShow()
                        }
                    }
                })

            }

        }
    });

    // 渲染PDF
    $('.file_box').on('click','a',function(e){
        e.preventDefault()
        e.stopPropagation()
        let purl = $(this).attr('href');//要展示的文件路径
        // console.log(purl)
        // let purl = 'JavaScript高级程序设计（第3版）中文 高清 完整.pdf'
        // 下面代码都是处理IE浏览器的情况
        if (window.ActiveXObject || "ActiveXObject" in window) {
            //判断是否为IE浏览器，"ActiveXObject" in window判断是否为IE11
            //判断是否安装了adobe Reader
            for (x = 2; x < 10; x++) {
                try {
                    oAcro = eval("new ActiveXObject('PDF.PdfCtrl." + x + "');");
                    if (oAcro) {
                        flag = true;
                    }
                } catch (e) {
                    flag = false;
                }
            }
            try {
                oAcro4 = new ActiveXObject('PDF.PdfCtrl.1');
                if (oAcro4) {
                    flag = true;
                }
            } catch (e) {
                flag = false;
            }

            try {
                oAcro7 = new ActiveXObject('AcroPDF.PDF.1');
                if (oAcro7) {
                    flag = true;
                }
            } catch (e) {
                flag = false;
            }

            if (flag) {//支持
                pdfShow(purl);//调用显示的方法

            }else {//不支持
                $("#pdfContent").append("对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装！");
                alert("对不起,您还没有安装PDF阅读器软件呢,为了方便预览PDF文档,请选择安装！");
                location = "http://ardownload.adobe.com/pub/adobe/reader/win/9.x/9.3/chs/AdbeRdr930_zh_CN.exe";
            }

        }else {
            pdfShow(purl);//调用显示的方法
            $("#PDFBox").addClass('active')
        }
    })
    // 显示pdf
    function pdfShow(url){
        $("#PDFBox").append('<iframe style="height:700px;width:700px;margin:0 auto;overflow: hidden" src="'+url+'"></iframe>');
    }

    // 当点击其他地方，关闭pdf
    //当点击区域不在列表选择范围内则关闭弹框
    $(document).click(function(e){
        var $this = $(e.target);
        if($this.parent().attr('id') != "PDFBox"){
            $('#PDFBox').removeClass('active');
            $("#PDFBox").html('')
        }
    });
    // 点击上传按钮
    $('.hidden_input').on('change',function(){
        let data = this.files;
        for(var i =0;i<data.length;i++){
            let fileReader = new FileReader();
            fileReader.readAsDataURL(data[i]);
            let size = Math.round(data[i].size/1024);
            let name = data[i].name;
            let spanHTML = `<span class="baogao_name">${name}</span><span class="baogao_size">${size}KB</span><i class="iconfont icon-delete delete_logo"></i>`;
            $(this).parent().siblings('span').html(spanHTML)
            $(this).attr('ValName',name)

            fileReader.onload = function (e){
                let url = this.result;//上传文件的URL
                Completed_file = []
                Completed_file.push(url);
                Completed_file.push(name);
                Completed_file.push(size);

            }
        }
    })

    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }
})